package de.fhtrier.mp.wollmaus.gamestate;

import org.newdawn.slick.Animation;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.state.transition.FadeInTransition;
import org.newdawn.slick.state.transition.FadeOutTransition;
import org.newdawn.slick.state.transition.Transition;

import de.fhtrier.mp.wollmaus.MainGame;
import de.fhtrier.mp.wollmaus.map.StageContainer;
import de.fhtrier.mp.wollmaus.util.AssetManager;

public class IntroState extends AbstractState {

	private int current = -1;
	private final int MAX_STEPS = 4;
	private Image img;
	private Animation shadow;
	
	private final int POINT_X = 925;
	private final int POINT_Y = 742;
	private Animation pointAni;

	public static IntroState instance;

	@Override
	public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
		instance = this;
		img = AssetManager.getInstance().getImage(AssetManager.IMAGE_INTRO_1);
		shadow = AssetManager.getInstance().getAnimation(AssetManager.ANI_INTRO_SHADOW);
		pointAni = AssetManager.getInstance().getAnimation(AssetManager.ANI_INTRO_POINT);
	}

	@Override
	public int getID() {
		return 2;
	}

	@Override
	public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
		img.draw();
		shadow.draw(596, 0);
		pointAni.draw(POINT_X, POINT_Y);
	}

	@Override
	public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
		super.update(gc, sbg, delta);

		for (int i = 0; i < Input.KEY_YEN; i++) {
			if (gc.getInput().isKeyDown(i)) {
				if (current == 0)
					MainGame.instance.bgMusic.stop();
				Transition transitionOut = new FadeOutTransition(Color.black, 1000);
				Transition transitionIn = new FadeInTransition(Color.black, 1000);

				if (StageContainer.getCurrentStageNumber() == 0) {
					MainGame.instance.enterState(1, transitionOut, transitionIn);
				}
				else {
					sbg.enterState(0, transitionOut, transitionIn);
				}
			}
		}
	}

	public void preEnter() {
		current++;
		current %= MAX_STEPS;
		img = AssetManager.getInstance().getImage(AssetManager.IMAGE_INTRO_1 + current);
		pointAni.restart();
		shadow.restart();
	}

	@Override
	public void enter(GameContainer gc, StateBasedGame sbg) throws SlickException {
	}

	@Override
	public void leave(GameContainer gc, StateBasedGame sbg) throws SlickException {
	}

}
